package com.fei.yuan.myapplication.slice;

import com.fei.yuan.myapplication.ResourceTable;
import com.fei.yuan.myapplication.utils.DateUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import static com.fei.yuan.myapplication.utils.ComponentProviderUtils.intToString;
import static com.fei.yuan.myapplication.utils.DateUtils.getWeek;

/**
 * @since 2021-07-21
 */
public class TimeCardSlice extends AbilitySlice {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, TimeCardSlice.class.getName());
    private static final Long PERIOD = 1000L;
    private Text monthText;
    private Text dayText;
    private Text hourText;
    private Text minText;
    private Text secondText;
    private Text weekText;
    private EventRunner eventRunner;
    private MyEventHandler myEventHandler;
    private Timer timer;
    private Runnable runnable = new Runnable() {
        private void initHandler() {
            eventRunner = EventRunner.getMainEventRunner();
            if (eventRunner == null) {
                return;
            }
            myEventHandler = new MyEventHandler(eventRunner);
        }

        @Override
        public void run() {
            initHandler();
        }
    };

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_form_image_with_info_time_2_2);

        initComponent();
        initData();
        startTimer();
    }

    // 计时器刷新数据
    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runnable.run();
                myEventHandler.sendEvent(1);
            }
        }, 0, PERIOD);
    }

    private void initComponent() {
        monthText = (Text) findComponentById(ResourceTable.Id_month);
        dayText = (Text) findComponentById(ResourceTable.Id_day);
        weekText = (Text) findComponentById(ResourceTable.Id_week);
        hourText = (Text) findComponentById(ResourceTable.Id_hour);
        minText = (Text) findComponentById(ResourceTable.Id_min);
        secondText = (Text) findComponentById(ResourceTable.Id_second);

    }

    private void initData() {
        //获取日期
        String currentDate = DateUtils.getCurrentDate("yyyy-MM-dd");
        String[] split = currentDate.split("-");
        monthText.setText(split[1]);
        dayText.setText(split[2]);

        //获取时间
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        hourText.setText(intToString(hour));
        int min = calendar.get(Calendar.MINUTE);
        minText.setText(intToString(min));
        int second = calendar.get(Calendar.SECOND);
        secondText.setText(intToString(second));

        //获取星期
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        weekText.setText(getWeek(this,week));

    }

    private class MyEventHandler extends EventHandler {

        public MyEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int eventId = event.eventId;
            if (eventId == 1) {
                initData();
            }
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        timer.cancel();
    }
}
